﻿(function (l, p) {
    if (!l.SearchBox) {
        l.SearchBox = {}
    }
    var h = l.SearchBox;
    var o = l.SearchBox.Util;
    var j = l.SearchBox.UI;
    var i = h.Tmpl;
    var g;
    var k;
    var n;
    var m = false;
    l.SearchBox.SearchEngines = {
        init: function () {
            this.renderEnabledSearchEngines();
            this.registerEvents()
        },
        registerEvents: function () {
            var q = this;
            $("#ruyitao-submit-ses-btn").click(function (r) {
                r.preventDefault();
                q.submit()
            })
        },
        getAll: function (q) {
            var r = this;
            l.browser.extension.sendRequest({
                topic: "get_search_engines"
            }, function (s) {
                g = s.search_engines;
                r.setEnabled(g);
                r.setLocale(s.locale);
                q(s)
            })
        },
        updateCached: function (v) {
            var s;
            var w;
            var r;
            for (var q in g) {
                if (g.hasOwnProperty(q)) {
                    s = g[q];
                    for (var u = 0, t = s.length; u < t; u++) {
                        w = s[u];
                        r = w.name;
                        if (v.hasOwnProperty(r)) {
                            w.enabled = v[r]
                        }
                    }
                }
            }
        },
        setLocale: function (q) {
            n = q;
            l.i18n.setLocale(q)
        },
        getLocale: function () {
            return n
        },
        getCachedAll: function () {
            return g
        },
        filter: function (r) {
            m = true;
            var v = l.site.get_site(p.location.hostname);
            var q = [];
            for (var t = 0, s = r.length; t < s; t++) {
                var u = r[t];
                if (u.name == "etao" || u.name == "ebay" || ((v == "paipai.com" || v == "eachnet.com") && u.name == "taobao")) { } else {
                    q.push(u)
                }
            }
            return q
        },
        sort: function (q) {
            q.sort(function (r, s) {
                return r.order - s.order
            })
        },
        renderSearchEngineSettingHtml: function () {
            var s = this.getCachedAll();
            var u = this.getLocale();
            var r = [];
            for (var q in s) {
                if (s.hasOwnProperty(q) && q == u) {
                    r = Array.prototype.concat.apply(r, s[q])
                }
            }
            r = this.filter(r);
            var t = i.getSearchEngineSettingTemplate(r);
            $("#ruyitao-ses-tbody").html(t)
        },
        renderEnabledSearchEngines: function () {
            var q = this.getEnabled();
            this.sort(q);
            var s = $("#ruyitao-search-engine-item-template").html();
            $.each(q, function (t, u) {
                u.icon = u.icon || o.getURL("assets/images/search/se-icons/" + u.name + ".png")
            });
            var r = i.getSearchEnginesTemplate(q);
            $("#ruyitao-search-engines").html(r)
        },
        setSearchEngineSettingPosition: function () {
            var q = $("#ruyitao-plus-btn");
            var s = $("#ruyitao-search-engines-setting");
            var r = q.position().left + q.width() / 2;
            var u = s.outerWidth();
            var t = r - u / 2;
            if (t < 0) {
                t = 0
            }
            s.css("left", t + "px")
        },
        setTitles: function () {
            $("#ruyitao-search-engines .ruyitao-se").each(function () {
                var s = $(this).attr("data-se-name");
                var q = h.App.getKeyword();
                var r = l.i18n.getMessage("search_product_in", s, q);
                $(this).attr("title", r)
            })
        },
        getEnabled: function () {
            return k
        },
        setEnabled: function (r) {
            r = r || this.getCachedAll();
            var u = l.site.get_site(p.location.hostname);
            var s = [];
            var t = function (w) {
                var y = r[w];
                var x, v;
                for (x = 0, v = y.length; x < v; x++) {
                    var z = y[x];
                    if (z.enabled && !(z.host && u.match(z.host))) {
                        if ((u == "paipai.com" || u == "eachnet.com") && (z.name == "taobao" || z.name == "etao")) { } else {
                            s.push(z)
                        }
                    }
                }
            };
            if (r[n]) {
                t(n)
            }
            for (var q in r) {
                if (r.hasOwnProperty(q) && q != n) {
                    t(q)
                }
            }
            k = s
        },
        submit: function () {
            var r = {};
            var v = 0;
            var q;
            var t;
            $("#ruyitao-ses-table .ruyitao-ses").each(function () {
                q = $(this).val();
                t = this.checked;
                r[q] = t;
                t && v++
            });
            if (m || v) {
                l.browser.extension.sendRequest({
                    topic: "set_search_engine",
                    search_engines: r
                });
                this.updateCached(r);
                this.setEnabled();
                this.renderEnabledSearchEngines();
                if (h.App.getKeyword()) {
                    this.setTitles()
                }
                j.hideSearchEngineSettingMsg();
                $("#ruyitao-plus-btn").trigger("click");
                var s = h.App;
                var u = s.getCurrentSearchEngine();
                if (!s.checkCurrentSearchEngineIsEnabled()) {
                    u = this.getEnabled()[0].name
                }
                s.selectSearchEngine(u)
            } else {
                j.showSearchEngineSettingMsg()
            }
        }
    }
})(a, document);